<template>
  <view class="cart-container" v-if="cart.length!==0">
    <my-address></my-address>
    <!-- 购物车主体区域 -->
    <view class="cart-body">
      <!-- 购物车大标题栏 -->
      <view class="cart-body-title">
        <uni-icons type="shop" size="26"></uni-icons>
        购物车
      </view>
      <!-- 购物车中各项商品 -->
      <uni-swipe-action>
        <block v-for="(goods,i) in cart" :key="i">
          <!-- <view class="goods-item" @click="gotoDetail(goods)"> -->
          <view class="goods-item">
            <uni-swipe-action-item :right-options="options" @click="bindClick($event,goods)">
              <my-goods :isCart="true" :goods="goods" @radio-change="radioChangeHandler" @num-change="numChangeHandler"></my-goods>
            </uni-swipe-action-item>
          </view>
        </block>
      </uni-swipe-action>
    </view>
    <!-- 结算组件 -->
    <my-settle></my-settle>
  </view>
  <!-- 购物车无商品时显示 -->
  <view class="empty-cart" v-else>
    <image src="../../static/empty_cart.png" class="empty-img" mode="widthFix"></image>
  </view>
</template>

<script>
  import {mapState,mapGetters,mapMutations} from 'vuex'
  import badgeMix from '../../mixins/tabbar-badge.js'
  export default {
    mixins:[badgeMix],
    computed:{
      ...mapState('m_cart',['cart']),
      ...mapGetters('m_cart',['total'])
    },
    data() {
      return {
        options:[
          {
            text:'删除',
            style:{
              backgroundColor:'#E16531'
            }
          }
        ]
      };
    },
    methods:{
      ...mapMutations('m_cart',['updateGoodsState','updateGoodsCount','deleteGoods']),
      // 商品选中状态发生改变
      radioChangeHandler(e){
        this.updateGoodsState(e)
      },
      // 商品数量发生改变
      numChangeHandler(e){
        this.updateGoodsCount(e)
        // var x=JSON.parse( uni.getStorageSync('cart'))
        // console.log(x)
        // console.log(e)
      },
      // 滑动模块中的按钮被点击
      bindClick(e,goods){
        if(e.content.text==="删除"){
          this.deleteGoods(goods)
        }
        // console.log(e)
        // console.log(goods)
      },
      // gotoDetail(goods){
      //   uni.navigateTo({
      //     url:'/subpkg/goods_detail/goods_detail?goods_id='+goods.goods_id
      //   })
      // }
    }
  }
</script>

<style lang="scss">
.cart-container{
  padding-bottom: 50px;
}
.cart-body-title{
  line-height: 40px;
  box-sizing: border-box;
  margin: 0 3px;
  border-bottom: 3px solid #fafafa;
  uni-icons{
    vertical-align: middle;
    margin:0 10px 0 5px;
  }
}
.empty-cart{
  display: flex;
  justify-content: center;
  align-items: center;
  .empty-img{
    width: 200px;
    padding: 100px;
  }
}
</style>
